[Previous] [Next]

Chapter 6

Classes and Objects

Since Microsoft Visual Basic 4 introduced the concept of class modules, a furious debate has raged among Visual Basic developers about the object-oriented nature of the language. Is Visual Basic a real object-oriented programming (OOP) language? Is it just an object-based language? Or is it somewhere between these two extremes?

For what it's worth, my position on the question is a compromise: Visual Basic definitively is not a true OOP language and it won't be one until it possesses some essential OOP features, such as inheritance. But this deficit shouldn't excuse your not learning in depth what classes and objects have to offer developers. This is what I'll show in this chapter and in the remainder of this book:

If you're absolutely new to object-oriented programming, this could be the most difficult chapter of the entire book for you to grasp. To understand how objects can help you write better programs in less time, you must be ready for a conceptual leap, not unlike the leap that many programmers had to take when switching from pure procedural MS-DOS languages such as QuickBasic to newer and more sophisticated event-driven programming environments such as Visual Basic. But once you grasp the basic concepts of OOP, you'll probably agree that objects are the most exciting thing to happen to Visual Basic since its first version. When you dive into object-oriented programming, you'll soon find yourself devising new, concise, and elegant solutions to old problems, often in less time and with less code. But I don't want to sound intimidating. As a Visual Basic programmer, you've already learned to master many advanced programming techniques concerned with, for example, events, database programming, and user interface issues. OOP isn't more difficult, it's merely different. And it's certainly a lot of fun.

If you've ever read books or articles about OOP, you surely found dozens of different definitions for the term object. Most of the definitions are correct and confusing at the same time. The definition I like most is this one:

An object is an entity that embeds both data and the code that deals with it.

Let's see what this means in practice.